<template>
  <div id="ForgetPwd2">
    <van-form @submit="onSubmit">
      <van-field
          type="password"
          v-model="password"
          name="password"
          label="密码"
          :rules="[{ required: true, message: '请填写密码' }]"
          :formatter="formatter"
      />
      <van-field
          type="password"
          v-model="rePassword"
          name="rePassword"
          label="确认密码"
          :rules="[{validator: rePasswordValidate, message: '两次密码输入不一致'}]"
          :formatter="formatter"
      />
      <div>
        <van-button plain :loading="isLoading" id="submitBtn" type="info" native-type="submit">
          提交
        </van-button>
      </div>
    </van-form>
  </div>
</template>

<script>
import {Toast} from 'vant';
import md5 from 'js-md5';

export default {
  name: 'ForgetPwd2',
  data() {
    return {
      email: '',
      password: '',
      rePassword: '',
      isLoading: false
    }
  },
  created() {
    this.checkStep();
  },
  methods: {
    onSubmit() {
      this.isLoading = true;
      return new Promise(((resolve, reject) => {
        return axios.post('/yibancircle/forg1etPsw', {
          email: this.email,
          password: this.password
        }, {
          headers: {'Content-type': 'application/json;charset=utf-8'}
        }).then(res => {
          if (res.status === 200) {
            Toast.success('密码修改成功');
            setTimeout(() => {
              localStorage.clear();
              this.$router.push('/login');
            }, 2000);
          }
          reject(res)
        }).catch(err => {
          reject(err)
        })
      })).catch(err => {
        Toast.fail('请刷新重新');
        setTimeout(() => {
          localStorage.clear();
          this.$router.push('/forgetpwd');
        }, 2000);
      })
    },
    checkStep() {
    //   setTimeout(() => {
    //     this.$router.push('/forgetpwd/1');
    //   }, 3000);
    // }
  },
  formatter(v) {
    return v.trim()
  },
  rePasswordValidate(val) {
    return this.password.trim() === val.trim();
  },
}
}
</script>

<style scoped>
#submitBtn {
  position: fixed;
  bottom: 0;
  width: 100%;
  z-index: 99;
}
</style>
